﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.ComponentModel.Composition;
using System.Windows;
using UserInterfaceLayer.Utils.WPF;

namespace AppToolkit.WPF.Services.UserMessageHandling.UI
{
    public class MessageViewHost : Control
    {
        private const string STR_Expanded = "expanded";
        private const string STR_Collapsed = "collapsed";

        IMessageVisualizer _visualizer;
        FlyoutWindow _flyoutWindow;

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);
            createFlyoutWindow();
        }

        [Import(typeof(IMessageVisualizer))]
        public IMessageVisualizer Visualizer
        {
            set
            {
                _visualizer = value;
                _visualizer.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(OnPropertyChanged); 
            }
        }

        void OnPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ShowInfo")
            {
                if (_visualizer.ShowInfo)
                {

                    if (_flyoutWindow.Owner == null)
                    {
                        _flyoutWindow.Owner = VisualTreeHelperExt.FindRootWindow(this);
                        _flyoutWindow.Show();
                    }
                    _flyoutWindow.SetVisualState(false);
                    //VisualStateManager.GoToElementState(_flyoutWindow, STR_Expanded, true);
                }
                else
                    _flyoutWindow.SetVisualState(true);
                    //VisualStateManager.GoToElementState(_flyoutWindow, STR_Collapsed, true); 
            }
        }

        private void createFlyoutWindow()
        {
            _flyoutWindow = new FlyoutWindow();
            _flyoutWindow.SetVisualState(true);
            //VisualStateManager.GoToElementState(_flyoutWindow, STR_Collapsed, false);
            //_flyoutWindow.IsKeyboardFocusWithinChanged += new DependencyPropertyChangedEventHandler(OnIsKeyboardFocusWithinChanged);

            _flyoutWindow.IsMouseDirectlyOverChanged += new DependencyPropertyChangedEventHandler(OnMouseOverChanged);
            _flyoutWindow._messageView.DataContext = _visualizer;
        }

        void OnMouseOverChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            _visualizer.IsFocused = (bool)e.NewValue;
        }

        //void OnIsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e)
        //{
        //    this._visualizer.IsFocused = (bool)e.NewValue;
        //}

    }
}
